package com.mrazola;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SpacePongView extends SurfaceView implements SurfaceHolder.Callback {

	private SurfaceHolder universe;
	private Ball ball;
	private Space space;
	private Pod pod;

	//TODO Put in Space or another object which is "Game State Aware"
	private int pressedKeysCount;

	public SpacePongView(Context context, AttributeSet attrs) {
		super(context, attrs);
		universe = this.getHolder();
		universe.addCallback(this);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO On surface changed...

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {

		space = new Space(universe, getContext());
		ball = new Ball(space, getContext());
		pod = new Pod(space, getContext());
		
		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {

				long previousFrame = System.currentTimeMillis();

				while(true) {

					long lastFrame = System.currentTimeMillis();
					long elapsedTime = lastFrame - previousFrame;
					//Log.d("ElapsedTime", "" + elapsedTime);
					try {
						Rect dirtyArea = space.getDirtyArea();
						Canvas sky = universe.lockCanvas(dirtyArea);
						
						space.updateBackgroundState(sky, elapsedTime);
						//ball.updateState(sky, elapsedTime);
						pod.updateState(sky, elapsedTime);
					
						universe.unlockCanvasAndPost(sky);
					} catch (Exception e) {
						break;
					}
					previousFrame = lastFrame;

				}
			}
		}, "painter");
		thread.start();

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Stop drawing thread on surface destroyed (Look at LunarView sample)

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		synchronized (space) {
			if (event.getRepeatCount() == 0) {
				if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
					pod.targetX = Float.NEGATIVE_INFINITY;
				} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
					pod.targetX = Float.POSITIVE_INFINITY;
				}	
				if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
					pod.movementEndTime = Long.MAX_VALUE;
					pod.movementStartTime = System.currentTimeMillis();
					pressedKeysCount++;
					return true;
				}
			}
			return false;
		}
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {	
		synchronized (space) {
			if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
				pressedKeysCount--;
				pod.movementEndTime = (pressedKeysCount <= 0) ? System.currentTimeMillis() : Long.MAX_VALUE;
				return true;
			}
			return false;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return handleMotionEvent(event);
	}

	@Override
	public boolean onTrackballEvent(MotionEvent event) {
		return handleMotionEvent(event);
	}

	//TODO Check if this method implementation could be appropiate in Player's Pod
	private boolean handleMotionEvent(MotionEvent event) {		
		synchronized (space) {
			//TODO Provide touch feedback
			//Register movement start time to Pod, only if it's the first press
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				pod.movementStartTime = System.currentTimeMillis();
			}
			//Never stop moving, always trying to get to the last touch x-coordinate
			pod.movementEndTime = Long.MAX_VALUE;
			pod.targetX = event.getX(event.getPointerCount() - 1);		
			return true;
		}
	}


}
